package com.renyi.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.google.gson.reflect.TypeToken;
import com.renyi.http.HttpResult;
import com.renyi.http.ParamBean;
import com.renyi.model.MedicineAppraise;
import com.renyi.model.MybatisService;
import com.renyi.model.RecipelDetail;
import com.renyi.util.JSONUtils;

/**
 * 
 * @author shs
 * 
 */
@Controller
public class DoctorRecipel extends BaseController {

	@Autowired
	private MybatisService mybatisService;

	// 医生-新增处方
	@RequestMapping("/newRecipel")
	public void newRecipel(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			result.setCode(0);
			Map<String, Object> paramMap = paramBean.getParam();
			// 登陆ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String name = paramMap.get("name") != null ? paramMap.get("name").toString() : "";
			String advice = paramMap.get("advice") != null ? paramMap.get("advice").toString() : "";
			String quantity = paramMap.get("quantity") != null ? paramMap.get("quantity").toString() : "1";
			String usage = paramMap.get("usage") != null ? paramMap.get("usage").toString() : "0";
			String type = paramMap.get("type") != null ? paramMap.get("type").toString() : "0";
			String jz = paramMap.get("jz") != null ? paramMap.get("jz").toString() : "0";
			String medicine = paramMap.get("medicine") != null ? paramMap.get("medicine").toString() : "";

			// if ("".equals(medicine)) {
			// result.setCode(-1); // 0：成功
			// result.setMsg("订单编号参数为空");
			// this.ajaxResultJson(response, result);
			// return;
			// }
			String jsondata = null;
			try {
				jsondata = JSON.toJSON(medicine).toString();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			TypeToken<ArrayList<RecipelDetail>> token = new TypeToken<ArrayList<RecipelDetail>>() {
			};
			List<RecipelDetail> mList = JSONUtils.fromJson(jsondata, token);

			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("id", 1);
			pMap.put("name", name);
			pMap.put("advice", advice);
			pMap.put("quantity", quantity);
			pMap.put("usage", usage);
			pMap.put("type", type);
			pMap.put("jz", jz);
			if(type.equals("0")){
				pMap.put("unit", "克");
			} else{
				pMap.put("unit", "盒");
			}
			
			this.mybatisService.insertIbatisObject("newRecipelSQL", pMap);
			Integer id = Integer.parseInt(pMap.get("id").toString());
			if (id > 0) {
				pMap.put("list", mList);
				pMap.put("recipelID", id);
				int ikey = (Integer) this.mybatisService.insertIbatisObject("newRecipelDetailSQL", pMap);
			} else {
				result.setCode(-1);
			}

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			this.ajaxResultJson(response, result);
		}

	}

	// 医生-获取处方列表
	@RequestMapping("getRecipelList")
	public void getRecipelList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";

			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("用戶ID参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("getRecipelListSQL", pMap);
			result.setCode(0); // 0：成功
			result.setData(data);
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 医生-获取处方详情
	@RequestMapping("getRecipelDetail")
	public void getRecipelDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String recipelID = paramMap.get("recipelID") != null ? paramMap.get("recipelID").toString() : "";

			if ("".equals(recipelID)) {
				result.setCode(-1); // 0：成功
				result.setMsg("处方详情编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("recipelID", recipelID);
			List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("getRecipelDetailSQL", pMap);
			result.setCode(0); // 0：成功
			result.setData(data);
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 删除处方
	@RequestMapping("/delRecipel")
	public void delRecipel(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String recipelID = paramMap.get("recipelID") != null ? paramMap.get("recipelID").toString() : "";

			if ("".equals(recipelID)) {
				result.setCode(-1); // 0：成功
				result.setMsg("处方详情编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("recipelID", recipelID);
			int key = (Integer) this.mybatisService.deleteIbatisObject("delRecipelSQL", pMap);
			if (key > 0) {
				result.setCode(0);
			} else {
				result.setCode(-1);
			}

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			this.ajaxResultJson(response, result);
		}
	}

	// 医生-编辑处方
	@RequestMapping("/editRecipel")
	public void editRecipel(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			result.setCode(0);
			Map<String, Object> paramMap = paramBean.getParam();
			// 登陆ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String recipelID = paramMap.get("recipelID") != null ? paramMap.get("recipelID").toString() : "";
			String name = paramMap.get("name") != null ? paramMap.get("name").toString() : "";
			String advice = paramMap.get("advice") != null ? paramMap.get("advice").toString() : "";
			String sum = paramMap.get("sum") != null ? paramMap.get("sum").toString() : "1";
			String usage = paramMap.get("usage") != null ? paramMap.get("usage").toString() : "0";
			String type = paramMap.get("type") != null ? paramMap.get("type").toString() : "0";
			String jz = paramMap.get("jz") != null ? paramMap.get("jz").toString() : "0";
			String medicine = paramMap.get("medicine") != null ? paramMap.get("medicine").toString() : "";
			String quantity = paramMap.get("quantity") != null ? paramMap.get("quantity").toString() : "1";

			if ("".equals(recipelID)) {
				result.setCode(-1); // 0：成功
				result.setMsg("处方编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}
			String jsondata = null;
			try {
				jsondata = JSON.toJSON(medicine).toString();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			TypeToken<ArrayList<RecipelDetail>> token = new TypeToken<ArrayList<RecipelDetail>>() {
			};
			List<RecipelDetail> mList = JSONUtils.fromJson(jsondata, token);

			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("recipelID", recipelID);
			pMap.put("name", name);
			pMap.put("advice", advice);
			pMap.put("sum", sum);
			pMap.put("usage", usage);
			pMap.put("type", type);
			pMap.put("jz", jz);
			pMap.put("quantity", quantity);
			if(type.equals("0")){
				pMap.put("unit", "克");
			} else{
				pMap.put("unit", "盒");
			}
			int key = (Integer) this.mybatisService.updateIbatisObject("editRecipelSQL", pMap);
			if (key > 0) {
				result.setCode(0);
				result.setMsg("");
				pMap.put("list", mList);
				int dkey = (Integer) this.mybatisService.deleteIbatisObject("delRecipelDetailSQL", pMap);
				int ikey = (Integer) this.mybatisService.insertIbatisObject("newRecipelDetailSQL", pMap);
			} else {
				result.setCode(-1);
				result.setMsg("");
			}

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			this.ajaxResultJson(response, result);
		}

	}

	// 医生-获取处方整剂用法(暂时不用)
	@RequestMapping("getRecipelUsage")
	public void getRecipelUsage(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			//处方类型 0 中药，1西药
			String type = paramMap.get("type") != null ? paramMap.get("type").toString() : "0";
			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("用戶ID参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("type", type);
			List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("getRecipelUsageSQL", pMap);
			result.setCode(0); // 0：成功
			result.setData(data);
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 医生-获取处方药品用法
	@RequestMapping("getRecipelMedicineUsage")
	public void getRecipelMedicineUsage(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			//药品用法  1中药 2西药 3用量 4频度、频率
			int type = paramMap.get("type") != null ?Integer.parseInt(paramMap.get("type").toString()) : 0;
			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("用戶ID参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>();
			if(type==1){
				data = this.mybatisService.findMybatisList("getRecipelMedicineUsageZYSQL",
						pMap);
			} else if(type==2) {
				data = this.mybatisService.findMybatisList("getRecipelMedicineUsageXYSQL",
						pMap);
			}else if(type==3) {
				data = this.mybatisService.findMybatisList("getRecipelMedicineUsageYLSQL",
						pMap);
			}else if(type==4) {
				data = this.mybatisService.findMybatisList("getRecipelMedicineUsagePLSQL",
						pMap);
			}
			
			result.setCode(0); // 0：成功
			result.setData(data);
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 医生-获取处方模板类型列表
	@RequestMapping("getRecipelType")
	public void getRecipelType(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";

			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("用戶ID参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("getRecipelTypeSQL", pMap);
			result.setCode(0); // 0：成功
			result.setData(data);
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 医生-获取处方模板详情
	@RequestMapping("RecipelTempletDetail")
	public void RecipelTempletDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String id = paramMap.get("id") != null ? paramMap.get("id").toString() : "";

			if ("".equals(id)) {
				result.setCode(-1); // 0：成功
				result.setMsg("处方详情编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("id", id);
			String recipelID = "";
			HashMap<String, Object> tMap = new HashMap<String, Object>();
			List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("RecipelTempletDetailSQL", pMap);
			if (data != null && data.size() > 0) {
				for (int i = 0; i < data.size(); i++) {
					tMap = data.get(i);
					recipelID = data.get(i).get("recipelID").toString().trim();
					if (!"".equals(recipelID)) {
						pMap.put("recipelID", recipelID);
						List<HashMap<String, Object>> list = this.mybatisService
								.findMybatisList("RecipelTempletDetailMSQL", pMap);
						tMap.put("medichine", list);
					}
				}
			}
			result.setCode(0); // 0：成功
			result.setData(data);
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 医生-获取处方主诉
	@RequestMapping("getRecipelZS")
	public void getRecipelZS(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			
			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("用戶ID参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("getRecipelZSSQL", pMap);
			result.setCode(0); // 0：成功
			result.setData(data);
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}
}
